<?php

namespace app\admin\controller;

use think\cache\driver\File;
use think\Controller;
use think\Db;
use think\Request;

class User extends Controller
{
    public function index()
    {
        $black = Db::name('blacklist')->select();
//        if($black)\
        $uid = [];
        foreach ($black as $user){
            $uid[] = $user['uid'];
        }
//        dump($uid);die;
        $user_list = Db::name('vip')
            ->select();
       return view('user/index',['data'=>$user_list,'uid'=>$uid]);
    }

    public function edit(Request $request)
    {
        $uid = $request->get('uid');
        $user_info = Db::name('vip')
            ->where('uid',$uid)
            ->find();
        return view('user/edit',['data'=>$user_info]);
    }

    public function add(Request $request)
    {
        return view();
    }

    public function doAdd()
    {
//        dump(input());
        $data = [
            'username'=>input('username'),
            'uname'=>input('uname'),
            'uemail'=>input('uemail'),
            'utel'=>input('utel'),
            'qq'=>input('qq'),
            'uage'=>input('uage'),
            'courseid'=>input('courseid'),
            'classid'=>input('classid'),
            'referid'=>input('referid'),
            'status'=>input('status')
        ];
        Db::name('vip')
            ->insert($data);
        $this->success('添加成功');
    }

    public function doEdit()
    {
        $im = request()->file('uimg');
        if(!empty($im)){
            $image = \think\Image::open($im);
        }
        if(isset($image)){
            $filename = rand(1000,9999).time();
            $font = ROOT_PATH . 'public' . DS .'static/admin/main/assets/fonts/fontawesome-webfont.ttf';
            $img = ROOT_PATH . 'public' . DS . 'uploads'.DS.date('Ymd').DS.$filename.'.jpg';
            $imgPath = date('Ymd').DS.$filename.'.jpg';
            $image->text('兄弟连“狠”爱你',$font,20,'#ff0000')->thumb(350,350,\think\Image::THUMB_CENTER)->save($img);
        }
        if(!isset($imgPath)){
            $data = [
                'uname'=>input('uname'),
                'uemail'=>input('uemail'),
                'qq'=>input('qq'),
                'uage'=>input('uage'),
                'courseid'=>input('courseid'),
                'classid'=>input('classid'),
                'status'=>input('status')
            ];
        }else{
            $data = [
                'uname'=>input('uname'),
                'uemail'=>input('uemail'),
                'qq'=>input('qq'),
                'uage'=>input('uage'),
                'courseid'=>input('courseid'),
                'classid'=>input('classid'),
                'uimg'=>$imgPath,
                'status'=>input('status')
            ];
        }
        $updateRes = Db::name('vip')
            ->where('uid',input('uid'))
            ->update($data);
        if($updateRes){
            $this->success('编辑成功');
        }else{
            $this->success('您没有做任何编辑');
        }
    }
    public function dodel()
    {
        $res = Db::name('blacklist')
            ->insert(['uid'=>input('uid')]);
//        $this->success('加入黑名单成功');
        if($res){
            return true;
        }

    }
}
